package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Setmeal;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        //1.调用service处理
        setmealService.saveWithDish(setmealDto);

        //2.响应数据
        return R.success("新增成功！");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> findPage(@RequestParam(defaultValue="1") Integer page, @RequestParam(defaultValue="10") Integer pageSize, String name){
        //1.调用service处理 返回Page<SetmealDto>
        Page<SetmealDto> setmealDtoPage =  setmealService.findPage(page,pageSize,name);

        //2.响应数据
        return R.success(setmealDtoPage);
    }

    //套餐删除
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids){
        //1.调用service处理
        setmealService.deleteByIds(ids);

        //2.响应数据
        return R.success("删除成功！");
    }

    //根据分类id查询套餐列表数据
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //1.调用service处理
        //1.1：创建wrapper对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        //1.2：设置查询条件 分类id和status
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        //1.3：调用service中的list方法查询 传入wrapper对象
        List<Setmeal> list = setmealService.list(wrapper);

        //2.响应数据
        return R.success(list);
    }


    //数据回显
    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id){

        return R.success(setmealService.findById(id));
    }


    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        //1.调用service处理
        setmealService.updateWithDishes(setmealDto);
        //2.响应数据
        return R.success("套餐修改成功~!");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,Long[] ids){

        log.info("【ids：{}】",ids);
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(Setmeal::getId,id);
            Setmeal setmeal =new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }


        return R.success("修改状态成功");
    }




}
